/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Carnet.java
 *
 * Created on 25-mar-2011, 17:09:10
 */

package presentacion.paciente;

import hibernate.objeto.Paciente;
import hibernate.objeto.Usuario;
import java.awt.Cursor;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.File;
import javax.swing.JEditorPane;
import javax.swing.JLabel;
import javax.swing.JPanel;
import logica.CapaLogica;
import logica.PacienteLogica;
import logica.UsuarioLogica;
import presentacion.CapaPresentacion;
import presentacion.acceso.RestriccionAcceso;
import presentacion.utilidad.ImagePanel;
import presentacion.utilidad.maquetado.Fuente;
import presentacion.utilidad.maquetado.Icono;

/**
 *
 * @author Administrador
 */
public class PacienteCarnetPresentacion extends javax.swing.JDialog {

    /** Creates new form Carnet */
    public PacienteCarnetPresentacion(java.awt.Frame parent, boolean modal,
            Paciente paciente) {
        super(parent, modal);
        initComponents();

        this.paciente = paciente;
        
        personalizarGUI();
        setearEventosQueCapturanTeclaEnterYEscape();
        this.setLocationRelativeTo(null);
    }

    /**
     * Setea los datos de la ventana.
     *
     */
    private void personalizarGUI() {
        //Setea el paciente
        usuario.setText(
                "HC nº: " + paciente.getIdPaciente() + " \n " +
                CapaPresentacion.imprimirApellidoYNombrePersona(paciente.getPersona()));

        //Setea la fotografía
        String path = CapaPresentacion.abrirArchivo(
                paciente.getIdPaciente(),
                paciente.getNombreArchivoFotografiaPaciente(),
                paciente.getArchivoFotografiaPaciente(),
                false);

        if (path == null) {
            //Se recupera el archivo del directorio donde se ecuentra el jar, se normaliza el espacio
            path = new File(CapaPresentacion.class.
                    getProtectionDomain().getCodeSource().getLocation().getPath()).
                    getParent().replaceAll("%20", " ");

            if (paciente.getSexoPaciente().equalsIgnoreCase("masculino")) {
                path = CapaLogica.reemplazarUnaBarraInvertidaPorDosBarrasInvertidas(
                    path + "\\imagenes\\no-foto-masculino.jpg");
            } else {
                path = CapaLogica.reemplazarUnaBarraInvertidaPorDosBarrasInvertidas(
                    path + "\\imagenes\\no-foto-femenina.jpg");
            }
        }
        
        try {
            ImagePanel ip = new ImagePanel();
            ip.setSize(panelFotografia.getSize());
            ip.loadImage(path);
            panelFotografia.add(ip);
            panelFotografia.revalidate();
            panelFotografia.repaint();
        } catch(Exception e) {
            System.err.println(e.getMessage());
        }

        //Setea datos de usuario
        Usuario usuarioPaciente = UsuarioLogica.getUsuarioPorDocumentoEnBd(
                paciente.getPersona().getTipoDeDocumentoPersona(),
                paciente.getPersona().getNumeroDeDocumentoPersona());
        nombreDeUsuario.setText(
                "Usuario: " +
                usuarioPaciente.getNombreDeUsuarioUsuario());
        claveDeUsuario.setText(
                "Clave: " +
                usuarioPaciente.getClaveUsuario());
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        claveDeUsuario = new javax.swing.JLabel();
        nombreDeUsuario = new javax.swing.JLabel();
        usuario = new javax.swing.JLabel();
        web = new javax.swing.JLabel();
        botonImprimir = new javax.swing.JButton();
        botonVolver = new javax.swing.JButton();
        panelFotografia = new javax.swing.JPanel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        claveDeUsuario.setFont(Fuente.FUENTE_TITULO_1);
        claveDeUsuario.setForeground(Fuente.COLOR_NORMAL);
        claveDeUsuario.setText("<User code>");
        claveDeUsuario.setPreferredSize(new java.awt.Dimension(75, 23));

        nombreDeUsuario.setFont(Fuente.FUENTE_TITULO_1);
        nombreDeUsuario.setForeground(Fuente.COLOR_NORMAL);
        nombreDeUsuario.setText("<User code>");
        nombreDeUsuario.setPreferredSize(new java.awt.Dimension(75, 23));

        usuario.setFont(Fuente.FUENTE_TITULO_1);
        usuario.setForeground(Fuente.COLOR_NORMAL);
        usuario.setText("<User code>");
        usuario.setPreferredSize(new java.awt.Dimension(75, 23));

        web.setFont(Fuente.FUENTE_HIPERVINCULO);
        web.setForeground(Fuente.COLOR_HIPERVINCULO);
        web.setText("www.hci2010.dyndns.org");
        web.setPreferredSize(new java.awt.Dimension(130, 23));
        web.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                webMouseEntered(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                webMouseExited(evt);
            }
        });

        botonImprimir.setFont(Fuente.FUENTE_BOTON_EXTERNO);
        botonImprimir.setForeground(Fuente.COLOR_NORMAL);
        botonImprimir.setIcon(Icono.ICONO_16_IMPRIMIR);
        botonImprimir.setText("Imprimir");
        botonImprimir.setToolTipText("Imprime el carnet del paciente");
        botonImprimir.setPreferredSize(new java.awt.Dimension(110, 23));
        botonImprimir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonImprimirActionPerformed(evt);
            }
        });

        botonVolver.setFont(Fuente.FUENTE_BOTON_EXTERNO);
        botonVolver.setForeground(Fuente.COLOR_NORMAL);
        botonVolver.setIcon(Icono.ICONO_16_CANCELAR);
        botonVolver.setText("Volver");
        botonVolver.setToolTipText("Cancela la operación");
        botonVolver.setPreferredSize(new java.awt.Dimension(110, 23));
        botonVolver.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonVolverActionPerformed(evt);
            }
        });

        panelFotografia.setPreferredSize(new java.awt.Dimension(160, 160));
        panelFotografia.setLayout(new javax.swing.BoxLayout(panelFotografia, javax.swing.BoxLayout.LINE_AXIS));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(web, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 121, Short.MAX_VALUE)
                        .addComponent(botonImprimir, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(botonVolver, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(layout.createSequentialGroup()
                                .addContainerGap()
                                .addComponent(usuario, javax.swing.GroupLayout.PREFERRED_SIZE, 299, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addGap(20, 20, 20)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(nombreDeUsuario, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 289, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(claveDeUsuario, javax.swing.GroupLayout.PREFERRED_SIZE, 289, javax.swing.GroupLayout.PREFERRED_SIZE))))
                        .addGap(18, 18, 18)
                        .addComponent(panelFotografia, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(usuario, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(11, 11, 11)
                        .addComponent(nombreDeUsuario, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(claveDeUsuario, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(panelFotografia, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(web, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(botonVolver, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(botonImprimir, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void webMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_webMouseEntered
        setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
}//GEN-LAST:event_webMouseEntered

    private void webMouseExited(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_webMouseExited
        setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
}//GEN-LAST:event_webMouseExited

    private void botonVolverActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonVolverActionPerformed
        dispose();
    }//GEN-LAST:event_botonVolverActionPerformed

    private void botonImprimirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonImprimirActionPerformed
        JEditorPane impresion = new JEditorPane();
        impresion.setText(PacienteLogica.getCarnetPacienteHTML(this));

        RestriccionAcceso.Imprimir(null, true, impresion, null);
    }//GEN-LAST:event_botonImprimirActionPerformed

    /**
     * Setea en cada componente del JDialog los eventos que capturarán la tecla
     * enter y escape.
     *
     */
    private void setearEventosQueCapturanTeclaEnterYEscape() {
        //Declaración del evento a capturar
        KeyListener keyListenerEscape = new KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                if (evt.getKeyCode() == KeyEvent.VK_ESCAPE) {
                    //Llamada al método que capturará el boton enter y
                    //trabajará sobre el mismo
                    dispose();
                }
            }
        };
        //Setear eventos
        botonVolver.addKeyListener(keyListenerEscape);
        botonImprimir.addKeyListener(keyListenerEscape);
    }

    public JLabel getClaveDeUsuario() {
        return claveDeUsuario;
    }

    public JLabel getNombreDeUsuario() {
        return nombreDeUsuario;
    }

    public Paciente getPaciente() {
        return paciente;
    }

    public JPanel getPanelFotografia() {
        return panelFotografia;
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton botonImprimir;
    private javax.swing.JButton botonVolver;
    private javax.swing.JLabel claveDeUsuario;
    private javax.swing.JLabel nombreDeUsuario;
    private javax.swing.JPanel panelFotografia;
    private javax.swing.JLabel usuario;
    private javax.swing.JLabel web;
    // End of variables declaration//GEN-END:variables
    private Paciente paciente;
}
